package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherIndexBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageOtherIndexRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

public class InStorageOtherIndexActivity extends BaseActivity {

    private TextView title, factory, inventoryAddress, moveType, specialInventory, moveTypeDescription, inStorageReason, supplierCode, supplierDescription, specialInventoryCode, specialInventoryDescription, nextStep;

    private TextView factoryValue, inventoryAddressValue, inStorageReasonValue;

    private PercentRelativeLayout factoryHide, moveTypeHide, supplierCodeHide, specialInventoryHide, specialInventoryCodeHide;

    private List<InStorageOtherIndexBean.DataBean.FactoryListBean> mFactoryList;

    private List<InStorageOtherIndexBean.DataBean.MoveListBean> mMoveList;

    public static final int SELECT_SUPPLIER = 100;

    public static final int SELECT_SPECIAL_INVENTORY = 200;

    private String mIsFocus;

    @Override

    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_index);
        initView(InStorageOtherIndexActivity.this);
        SPUtils.removeOtherInStorageInformation(InStorageOtherIndexActivity.this);
        initData(InStorageOtherIndexActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        factory = (TextView) activity.findViewById(R.id.factory);
        inventoryAddress = (TextView) activity.findViewById(R.id.inventoryAddress);
        moveType = (TextView) activity.findViewById(R.id.moveType);
        specialInventory = (TextView) activity.findViewById(R.id.specialInventory);
        moveTypeDescription = (TextView) activity.findViewById(R.id.moveTypeDescription);
        inStorageReason = (TextView) activity.findViewById(R.id.inStorageReason);
        supplierCode = (TextView) activity.findViewById(R.id.supplierCode);
        supplierDescription = (TextView) activity.findViewById(R.id.supplierDescription);
        specialInventoryCode = (TextView) activity.findViewById(R.id.specialInventoryCode);
        specialInventoryDescription = (TextView) activity.findViewById(R.id.specialInventoryDescription);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);

        factoryValue = (TextView) activity.findViewById(R.id.factoryValue);
        inventoryAddressValue = (TextView) activity.findViewById(R.id.inventoryAddressValue);
        inStorageReasonValue = (TextView) activity.findViewById(R.id.inStorageReasonValue);

        factoryHide = (PercentRelativeLayout) activity.findViewById(R.id.factoryHide);
        moveTypeHide = (PercentRelativeLayout) activity.findViewById(R.id.moveTypeHide);
        supplierCodeHide = (PercentRelativeLayout) activity.findViewById(R.id.supplierCodeHide);

        specialInventoryHide = (PercentRelativeLayout) activity.findViewById(R.id.specialInventoryHide);
        specialInventoryCodeHide = (PercentRelativeLayout) activity.findViewById(R.id.specialInventoryCodeHide);
    }

    private void initData(Activity activity) {
        title.setText("其他入库");
        ActivityUtils.initBackAndFunction(activity, "其他", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }

    private void getData(Context context) {
        factoryHide.setVisibility(View.GONE);
        moveTypeHide.setVisibility(View.GONE);
        supplierCodeHide.setVisibility(View.GONE);
        specialInventoryCodeHide.setVisibility(View.GONE);
        InStorageOtherIndexRequest.request(context, new GsonBeanListener<InStorageOtherIndexBean>() {
            @Override
            public void onSuccess(InStorageOtherIndexBean bean) {
                mFactoryList = bean.getData().getFactoryList();
                mMoveList = bean.getData().getMoveList();
                mIsFocus = bean.getData().getIsFocus();
                initSpinner(context);
            }
        });
        supplierCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InStorageOtherSelectSupplierActivity.class);
                startActivityForResult(intent, SELECT_SUPPLIER);
            }
        });

        specialInventoryCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InStorageOtherSelectSpecialInventoryActivity.class);
                startActivityForResult(intent, SELECT_SPECIAL_INVENTORY);
            }
        });

        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String checkResult = check(context);
                if (checkResult == null) {
                    updateInfo(context);
                    Intent intent = new Intent(context, InStorageOtherMaterielOverviewActivity.class);
                    startActivity(intent);
                } else {
                    Utils.middleToast(context, checkResult);
                }

            }
        });
    }

    private void updateInfo(Context context) {
        InStorageOtherBean bean = InStorageUtils.getInStorageOtherBean(context);
        bean.setIsFocus(mIsFocus);
        bean.setFactoryCode(factory.getText().toString().split(" - ")[0]);
        bean.setFactoryValue(factoryValue.getText().toString());
        bean.setInventoryAddressCode(inventoryAddress.getText().toString().split(" - ")[0]);
        bean.setInventoryAddressValue(inventoryAddressValue.getText().toString());
        bean.setMoveType(moveType.getText().toString());
        bean.setSpecialInventory(specialInventory.getText().toString());
        bean.setMoveDescription(moveTypeDescription.getText().toString());
        bean.setInStorageReasonCode(inStorageReason.getText().toString().split(" - ")[0]);
        bean.setInStorageReasonValue(inStorageReasonValue.getText().toString());
        bean.setSupplierCode(supplierCode.getText().toString());
        bean.setSupplierDescription(supplierDescription.getText().toString());
        bean.setSpecialInventoryCode((specialInventoryCode.getText().toString().equals(context.getResources().getString(R.string.activity_in_storage_other_index_special_inventory_code_hint))) ? "" : specialInventoryCode.getText().toString());
        bean.setSpecialInventoryDescription(specialInventoryDescription.getText().toString());
        InStorageUtils.saveInStorageOtherBean(context, bean);
    }

    private String check(Context context) {
        if ("".equals(factoryValue.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_factory_hint);
        }
        if ("".equals(inventoryAddressValue.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint);
        }
        if (context.getResources().getString(R.string.activity_in_storage_other_index_move_type_hint).equals(moveType.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_move_type_hint);
        }
        if ("".equals(inStorageReasonValue.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_in_storage_reason_hint);
        }
        if (context.getResources().getString(R.string.activity_in_storage_other_index_supplier_hint).equals(supplierCode.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_supplier_hint);
        }
        if(!TextUtils.isEmpty(specialInventory.getText().toString())){
            if (context.getResources().getString(R.string.activity_in_storage_other_index_special_inventory_code_hint).equals(specialInventoryCode.getText().toString())) {
                return context.getResources().getString(R.string.activity_in_storage_other_index_special_inventory_code_hint);
            }
        }
        return null;
    }

    private void resetViewWithoutFactory(Context context) {
        inventoryAddress.setText(context.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint));
        inventoryAddressValue.setText("");
        moveType.setText(context.getResources().getString(R.string.activity_in_storage_other_index_move_type_hint));
        specialInventory.setText("");
        moveTypeDescription.setText("");
        inStorageReason.setText(context.getResources().getString(R.string.activity_in_storage_other_index_in_storage_reason_hint));
        supplierCode.setText(context.getResources().getString(R.string.activity_in_storage_other_index_supplier_hint));
        supplierDescription.setText("");
        specialInventoryCode.setText(context.getResources().getString(R.string.activity_in_storage_other_index_special_inventory_code_hint));
        specialInventoryDescription.setText("");

        factoryHide.setVisibility(View.GONE);
        moveTypeHide.setVisibility(View.GONE);
        supplierCodeHide.setVisibility(View.GONE);
        specialInventoryHide.setVisibility(View.GONE);
        specialInventoryCodeHide.setVisibility(View.GONE);
    }

    private void initSpinner(Context context) {
        factory.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getFactoryDialog(context, mFactoryList, new DialogUtils.FactoryListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String value, List<InStorageOtherIndexBean.DataBean.FactoryListBean.InventoryAddressListBean> inventoryAddressList) {
                        if (!text.equals(factory.getText().toString())) {
                            factory.setText(text);
                            factoryValue.setText(value);
                            resetViewWithoutFactory(context);
                            factoryHide.setVisibility(View.VISIBLE);
                            inventoryAddress.setOnClickListener(new NoDoubleClickListener() {
                                @Override
                                public void onNoDoubleClick(View v) {
                                    DialogUtils.getInventoryAddressDialog(context, inventoryAddressList, new DialogUtils.SelectListener() {
                                        @Override
                                        public void onSelect(Dialog dialog, String text, String value) {
                                            inventoryAddress.setText(text);
                                            inventoryAddressValue.setText(value);
                                        }
                                    }).show();
                                }
                            });
                        }
                    }
                }).show();
            }
        });
        moveType.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getMoveDialog(context, mMoveList, new DialogUtils.MoveListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String rSpecialInventory, String rMoveTypeDescription, List<InStorageOtherIndexBean.DataBean.MoveListBean.InStorageReasonListBean> reasonList) {
                        moveType.setText(text);
                        specialInventory.setText(rSpecialInventory);
                        moveTypeDescription.setText(rMoveTypeDescription);
                        if (!TextUtils.isEmpty(rSpecialInventory)) {
                            specialInventoryHide.setVisibility(View.VISIBLE);
                        } else {
                            specialInventoryHide.setVisibility(View.GONE);
                        }
                        moveTypeHide.setVisibility(View.VISIBLE);
                        inStorageReason.setOnClickListener(new NoDoubleClickListener() {
                            @Override
                            public void onNoDoubleClick(View v) {
                                DialogUtils.getInStorageReasonDialog(context, reasonList, new DialogUtils.SelectListener() {
                                    @Override
                                    public void onSelect(Dialog dialog, String text, String value) {
                                        inStorageReason.setText(text);
                                        inStorageReasonValue.setText(value);
                                    }
                                }).show();
                            }
                        });
                    }
                }).show();
            }
        });
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            switch (requestCode) {
                case SELECT_SUPPLIER:
                    supplierCode.setText(data.getStringExtra(InStorageExtra.CODE));
                    supplierDescription.setText(data.getStringExtra(InStorageExtra.DESCRIPTION));
                    supplierCodeHide.setVisibility(View.VISIBLE);
                    break;
                case SELECT_SPECIAL_INVENTORY:
                    specialInventoryCode.setText(data.getStringExtra(InStorageExtra.CODE));
                    specialInventoryDescription.setText(data.getStringExtra(InStorageExtra.DESCRIPTION));
                    specialInventoryCodeHide.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(InStorageOtherIndexActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                InStorageOtherIndexActivity.super.onBackPressed();
            }
        }).show();
    }
}
